package pms.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pms.om.Kategorie;
import pms.om.Noten;
import pms.om.Person;
import pms.om.PersonHatNoten;

public class PersonHatNotenDao extends HibernateDaoSupport {
	private PersonHatNotenDao personHatNotenDao;
	private PersonDao personDao;
	private NotenDao notenDao;

	/** Getter
	 * @return the personHatNotenDao
	 */
	public PersonHatNotenDao getPersonHatNotenDao() {
		return personHatNotenDao;
	}

	/** Setter
	 * @param personHatNotenDao the personHatNotenDao to set
	 */
	public void setPersonHatNotenDao(PersonHatNotenDao personHatNotenDao) {
		this.personHatNotenDao = personHatNotenDao;
	}

	/** Getter
	 * @return the personDao
	 */
	public PersonDao getPersonDao() {
		return personDao;
	}

	/** Setter
	 * @param personDao the personDao to set
	 */
	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	/** Getter
	 * @return the notenDao
	 */
	public NotenDao getNotenDao() {
		return notenDao;
	}

	/** Setter
	 * @param notenDao the notenDao to set
	 */
	public void setNotenDao(NotenDao notenDao) {
		this.notenDao = notenDao;
	}

	/** findById
	 * @param id
	 * @return the PersonHatNoten
	 */
	public PersonHatNoten findById (int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (PersonHatNoten) template.get(PersonHatNoten.class, id);
	}
	
	/** findAll
	 * @return the results
	 */
	@SuppressWarnings("unchecked")
	public List<PersonHatNoten> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		List<PersonHatNoten> results = template.loadAll(PersonHatNoten.class);
		return results;
	}
	
	/** create
	 * @param ausleihdatum
	 * @param rueckgabedatum
	 * @param noten
	 * @param person
	 */
	public void create(Date ausleihdatum, Date rueckgabedatum,
			Noten noten, Person person) {
		HibernateTemplate template = getHibernateTemplate();
		PersonHatNoten phn = new PersonHatNoten(ausleihdatum, rueckgabedatum, noten, person);
		template.persist(phn);
	}
	
	/** modify
	 * @param id
	 * @param ausleihdatum
	 * @param rueckgabedatum
	 * @param noten
	 * @param person
	 */
	public void modify(int id, Date ausleihdatum, Date rueckgabedatum,
			Noten noten, Person person) {
		HibernateTemplate template = getHibernateTemplate();
		PersonHatNoten phn = (PersonHatNoten) template.get(PersonHatNoten.class, id);
		phn.setAusleihdatum(ausleihdatum);
		phn.setRuckgabedatum(rueckgabedatum);
		phn.setNoten(noten);
		phn.setPerson(person);
		template.saveOrUpdate(phn);
	}
	
	/** deleteById
	 * @param id
	 * @throws Exception
	 */
	public void deleteById(int id)throws Exception {
		HibernateTemplate template = getHibernateTemplate();
		PersonHatNoten phn = findById(id);
			template.delete(phn);
		template.flush();
	}
}
